#include "fileoperations.h"

#include <fstream>
#include <vector>

using namespace std;

bool FileOperations::readTextFile(string& contents, const string& filename)
{
    int length = 0;
    ifstream fileStream(filename.c_str(), ifstream::binary);

    if (!fileStream.is_open())
    {
        return false;
    }

    fileStream.seekg(0, ios::end);
    length = fileStream.tellg();
    fileStream.seekg (0, ios::beg);

    vector<char> buffer(length + 1);
    fileStream.read(&(buffer.front()), length);
	buffer[length] = 0;
    contents = &(buffer.front());

    fileStream.close();
    return true;
}

string FileOperations::getFileExtension(const string& filename)
{
    string extension;
    
    string::size_type pos = filename.find_last_of('.');
    if (pos != string::npos && pos != filename.size())
    {
        extension = filename.substr(pos + 1, filename.size());
    }
    
    return extension;
}
